<script setup>
  import { ref } from 'vue'
  import { verifyCodeApi, loginByMobileApi } from '@/services/user.js'
  import { useUserStore } from '@/store/user.js'
  import defaults from 'luch-request/src/lib/core/defaults'
  const user = useUserStore()
  const showCountDown = ref(false)
  const formRef = ref(null)
  const formData = ref({
    mobile: '13230000019',
    code: ''
  })
  const isChecked = ref(false)
  const formRules = {
    mobile: {
      rules: [
        { required: true, errorMessage: '请填写手机号码' },
        { pattern: '^1\\d{10}$', errorMessage: '手机号码格式不正确' }
      ]
    },
    code: {
      rules: [
        { required: true, errorMessage: '请输入验证码' },
        { pattern: '^\\d{6}$', errorMessage: '验证码格式不正确' }
      ]
    }
  }
  const getCode = async () => {
    await formRef.value.validateField(['mobile'])
    showCountDown.value = true
    const res = await verifyCodeApi({
      mobile: formData.value.mobile,
      type: 'login'
    }).catch((err) => {
      showCountDown.value = false
      return Promise.reject(err)
    })
    formData.value.code = res.data.data.code
    uni.utils.toast('已发送，请查收')
  }
  //登录
  const onFormSubmit = async () => {
    await formRef.value.validate()
    if (!isChecked.value) return uni.utils.toast('未同意勾选')
    const res = await loginByMobileApi(formData.value)
    user.$patch({
      token: res.data.data.token,
      refreshToken: res.data.data.refreshToken,
      returnUrl: '/pages/index/index',
      returnType: 'switchTab',
      userId: res.data.data.id
    })
    uni[user.returnType]({
      url: user.returnUrl
    })
  }
</script>
<template>
  <uni-forms
    class="login-form"
    :model="formData"
    :rules="formRules"
    ref="formRef"
  >
    <uni-forms-item name="mobile">
      <uni-easyinput
        v-model="formData.mobile"
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
      />
    </uni-forms-item>
    <uni-forms-item name="code">
      <uni-easyinput
        v-model="formData.code"
        :input-border="false"
        :clearable="false"
        placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5"
      />

      <view v-if="showCountDown" style="display: flex">
        <!-- <my-countdown
          :show-day="false"
          :show-hours="false"
          :show-minute="false"
          :second="5"
          color="#16C2A3"
          @timeup="showCountDown = false"
        ></my-countdown>
        秒后重新获取 -->
        <my-countdown2 @timeup="showCountDown = false"></my-countdown2>
        秒后重新获取
      </view>
      <text v-else class="text-button" @click="getCode">获取验证码</text>
    </uni-forms-item>

    <view class="agreement" @click="isChecked = !isChecked">
      <radio
        :checked="isChecked"
        color="#16C2A3"
        @click.stop="isChecked = !isChecked"
      />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="onFormSubmit">登 录</button>
  </uni-forms>
</template>

<script>
  export default {
    options: {
      styleIsolation: 'shared'
    }
  }
</script>

<style lang="scss">
  @import './styles.scss';
</style>
